package com.xbai.intervention.service;

import com.netflix.hystrix.*;
import com.xbai.intervention.client.UserMapper;
import com.xbai.intervention.client.UserServiceClient;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/15 0015
 */
public class GetUserCommand extends HystrixCommand<UserMapper> {

    /**
     * 远程调用 user-service 的客户端工具类
     */
    private UserServiceClient userServiceClient;

    protected GetUserCommand(String name) {
        super(
                Setter.withGroupKey(
                        // 设置命令组
                        HystrixCommandGroupKey.Factory.asKey("springHealthGroup"))
                        // 设置命令键
                        .andCommandKey(HystrixCommandKey.Factory.asKey("interventionKey"))
                        // 设置线程池键
                        .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(name))
                        // 设置命令属性
                        .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(5000))
                        // 设置线程池属性
                        .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withMaxQueueSize(10).withCoreSize(2))
        );
    }

    @Override
    protected UserMapper run() throws Exception {
        return userServiceClient.getUserByUserName("spring-health-user");
    }

    @Override
    protected UserMapper getFallback() {
        return new UserMapper(1L, "user1", "spring-health-user");
    }
}
